import pymysql
from submodules.common.lib.log_ import logger

from config.db import mysql_config


def select(sql_):
    try:
        db = pymysql.connect(**mysql_config)
        cursor = db.cursor()
        cursor.execute(sql_)
        res = cursor.fetchall()
        db.close()
        return True, res
    except Exception as e:
        db.close()
        return False, e.__repr__()


def insert(sql_):
    try:
        db = pymysql.connect(**mysql_config)
        cursor = db.cursor()
        cursor.execute(sql_)
        db.commit()
        db.close()
        return True
    except Exception as e:
        db.rollback()
        db.close()
        return e.__repr__()


def delete(sql_):
    insert(sql_)


def update(sql_):
    insert(sql_)


if __name__ == "__main__":
    def test_select():
        sql = "SELECT * FROM `user_tmp`;"
        sql = "SELECT * FROM house_information.court  limit 100;"
        res = select(sql)
        print(res)


    def test_insert():
        sql = 'INSERT INTO house_information.user_tmp (id,f_name,age,create_time) VALUES (DEFAULT, "han", 18, 1630518236396);'
        res = insert(sql)
        print(res)


    test_select()
